package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import lombok.Data;

import static com.shanxia.codec.Decoder.byteToAscii;
import static com.shanxia.codec.Decoder.encodeAscii;

/**
 * S -> C
 * 服务器下发升级文件名指令
 */
@Data
public class Cmd1003 extends ProtocolDataBase {

    private String fileName;                // 文件名

    public Cmd1003(){
        super.setCmd(1003);
    }

    public static Cmd1003 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1003 cmd = new Cmd1003();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        cmd.fileName = byteToAscii(dataField, false);
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[this.fileName.length()];
        encodeAscii(data, this.fileName, this.fileName.length(), 0);
        return data;
    }
}
